package shop.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import shop.dao.GoodsDao;
import shop.entity.Goods;
import shop.util.DBUtil;

public class GoodsDaoImpl implements GoodsDao {

	@Override
	public List<Goods> findAll() {
		List<Goods> list = new ArrayList<>();
		String sql = "select * from t_goods limit 8";
		ResultSet rs = DBUtil.query(sql, null);
		try {
			while(rs.next()) {
				int id = rs.getInt("id");
				String goodsName = rs.getString("goods_name");
				String goodsDesc = rs.getString("goods_desc");
				double price = rs.getDouble("price");
				int stock = rs.getInt("stock");
				int classifyLevel1Id = rs.getInt("classify_level1_id");
				int classifyLevel2Id = rs.getInt("classify_level2_id");
				int classifyLevel3Id = rs.getInt("classify_level3_id");
				String fileName = rs.getString("file_name");
				int isDelete = rs.getInt("is_delete");
				String img1 = rs.getString("img1");
				String img2 = rs.getString("img2");
				String img3 = rs.getString("img3");
				String img4 = rs.getString("img4");
				String img5 = rs.getString("img5");
				String img6 = rs.getString("img6");
				String img7 = rs.getString("img7");
				Goods goods = new Goods(id, goodsName, goodsDesc, price, stock, classifyLevel1Id, classifyLevel2Id, classifyLevel3Id, fileName, isDelete, img1, img2, img3, img4, img5, img6, img7);
				list.add(goods);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Goods findById(int id) {
		Goods goods = null;
		String sql = "select * from t_goods where id = "+id;
		ResultSet rs = DBUtil.query(sql, null);
		try {
			while(rs.next()) {
				int id1 = rs.getInt("id");
				String goodsName = rs.getString("goods_name");
				String goodsDesc = rs.getString("goods_desc");
				double price = rs.getDouble("price");
				int stock = rs.getInt("stock");
				int classifyLevel1Id = rs.getInt("classify_level1_id");
				int classifyLevel2Id = rs.getInt("classify_level2_id");
				int classifyLevel3Id = rs.getInt("classify_level3_id");
				String fileName = rs.getString("file_name");
				int isDelete = rs.getInt("is_delete");
				String img1 = rs.getString("img1");
				String img2 = rs.getString("img2");
				String img3 = rs.getString("img3");
				String img4 = rs.getString("img4");
				String img5 = rs.getString("img5");
				String img6 = rs.getString("img6");
				String img7 = rs.getString("img7");
				goods = new Goods(id, goodsName, goodsDesc, price, stock, classifyLevel1Id, classifyLevel2Id, classifyLevel3Id, fileName, isDelete, img1, img2, img3, img4, img5, img6, img7);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return goods;
	}

}
